package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ums.entity.MemberAccount;
import cn.stylefeng.guns.modular.ums.entity.MemberAccountRecords;
import cn.stylefeng.guns.modular.ums.model.params.MemberAccountParam;
import cn.stylefeng.guns.modular.ums.model.params.MemberAccountRecordsParam;
import cn.stylefeng.guns.modular.ums.model.params.MemberWithdrawParam;
import cn.stylefeng.guns.modular.ums.model.result.MemberAccountRecordsResult;
import cn.stylefeng.guns.modular.ums.model.result.MemberAccountResult;
import cn.stylefeng.guns.modular.ums.model.result.MemberWithdrawResult;
import cn.stylefeng.guns.modular.ums.service.MemberAccountRecordsService;
import cn.stylefeng.guns.modular.ums.service.MemberAccountService;
import cn.stylefeng.guns.modular.ums.service.MemberWithdrawService;
import cn.stylefeng.guns.modular.wms.model.params.ApplyLogParam;
import cn.stylefeng.guns.modular.wms.model.params.ApplyReadParam;
import cn.stylefeng.guns.modular.wms.model.result.ApplyLogResult;
import cn.stylefeng.guns.modular.wms.service.ApplyLogService;
import cn.stylefeng.guns.modular.wms.service.ApplyReadService;
import cn.stylefeng.guns.utils.DateUtils;
import cn.stylefeng.guns.utils.MemberTokenUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 财务管理模块
 */
@Controller
@RequestMapping("/api/finance")
@Api(tags = "财务管理模块")
public class ApiFinanceController extends BaseController {

    @Autowired
    private MemberAccountRecordsService accountRecordsService;
    @Autowired
    private MemberAccountService accountService;
    @Autowired
    private MemberWithdrawService withdrawService;

    /**
     * 查询收支明细
     */
    @ResponseBody
    @RequestMapping(value = "/getFinanceList", method = RequestMethod.GET)
    @ApiOperation(value = "查询收支明细", notes = "查询收支明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getApplyList(MemberAccountRecordsParam param) {
        int memberId = MemberTokenUtil.getMemberId();
        param.setMemberId(memberId);
        LayuiPageInfo pageInfo = accountRecordsService.findPageBySpec(param);
        return ResponseData.success(pageInfo);
    }


    /**
     * 查询提现记录
     */
    @ResponseBody
    @RequestMapping(value = "/getWithdrawList", method = RequestMethod.GET)
    @ApiOperation(value = "查询提现记录", notes = "查询提现记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getWithdrawList(MemberWithdrawParam param) {
        int memberId = MemberTokenUtil.getMemberId();
        param.setMemberId(memberId);
        LayuiPageInfo pageInfo = withdrawService.findPageBySpec(param);
        return ResponseData.success(pageInfo);
    }

    /**
     * 查询账户余额
     */
    @ResponseBody
    @RequestMapping(value = "/getAccountDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询账户余额", notes = "查询账户余额")
    public ResponseData getApplyDetail() {
        try {
            int memberId = MemberTokenUtil.getMemberId();
            MemberAccountParam param = new MemberAccountParam();
            param.setMemberId(memberId);
            MemberAccountResult result = accountService.findBySpec(param);
            if (result==null){
                result=new MemberAccountResult();
                //创建会员账户信息
                MemberAccountParam accountParam=new MemberAccountParam();
                accountParam.setMemberId(memberId);
                accountService.add(accountParam);
                result.setBalance(BigDecimal.ZERO);
                result.setBalanceWithdraw(BigDecimal.ZERO);
                result.setBalanceFrozen(BigDecimal.ZERO);
            }
            //查询本月收入
            String month=DateUtils.getCurrentTime("yyyy-MM");
            MemberAccountRecordsResult accountRecordsResult=accountRecordsService.getStatistics(new MemberAccountRecordsParam().setMemberId(memberId).setStartDate(month));
            if (accountRecordsResult!=null){
                result.setTotalMoney(accountRecordsResult.getMoney());
            }else{
                result.setTotalMoney(BigDecimal.ZERO);
            }
            return ResponseData.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error(e.getMessage());
        }
    }

    /**
     * 提现申请
     */
    @ResponseBody
    @RequestMapping(value = "/withdrawApply", method = RequestMethod.POST)
    @ApiOperation(value = "提现申请", notes = "提现申请")
    public ResponseData withdrawApply(@RequestBody MemberWithdrawParam param) {
        try {
            int memberId = MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
            withdrawService.add(param);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.error(e.getMessage());
        }
    }

}



